
#ifndef _TTG_ERRORS_
#define _TTG_ERRORS_
#include "Common/Definitions.h"

#ifdef MICROSOFT_DIRECTX
#include <d3dx9.h>
#include <dxerr9.h>
#else
#include <windows.h>
#endif

#include <stdio.h>
#include <string>
#include <exception>

namespace ttg
{
	//Definition of used class.
	namespace services
	{
		class Logger;
	}

	//Basic Error.
	struct Error :public std::exception
	{
		private:
			std::string object;
			std::string method;
			std::string description;
			int level;
		public:
			//Describes global error. Engine must be halted or restarted.
			const static int FATAL = 1;
			//Local error. Engine is alive, but it can work unproperly.
			const static int LOCAL = 2;
			//Just warning. For debug purposes only.
			const static int WARNING = 3;
		public:
			//Creates Error with given description.
			inline Error(int level, const char *description)
			{
				this->level = level;
				this->description = description;
			}
			//Creates Error with given description and location.
			Error(int level,
				  const char *object,
				  const char *method,
				  const char *description)
			{
				this->level = level;
				this->object = object;
				this->method = method;
				this->description = description;
			}
			//Creates Error with given enhanced description and location.
			Error(int level,
				  const char *object,
				  const char *method,
				  const char *description1,
				  const char *description2)
			{
				this->level = level;
				this->object = object;
				this->method = method;
				this->description = std::string(description1) + "(" + description2 + ")";
			}
			//Creates Error with given local-description, Win32-description and location.
			Error(int level,
				  const char *object,
				  const char *method,
				  const char *description,
				  HRESULT err)
			{
				this->level = level;
				this->object = object;
				this->method = method;
	#ifdef MICROSOFT_DIRECTX
				this->description = std::string(description) + "(" + DXGetErrorDescription9A(err) + ")";
	#else
				this->description = std::string(description);
	#endif
			}

			//Returs level of error (warning, local or fatal error).
			inline int getLevel() const
			{ return level; }
			//Returns string description of the error.
			virtual char const *toString() const
			{ return description.data(); }
			//Calls toString() method.
			virtual const char *what() const
			{ return toString(); }
			//For descendents only.
			virtual ~Error()
			{ /*nothing*/ }
		friend class ttg::services::Logger;
	};
}

namespace ttg_internal
{
	//Definition of the errors, that will be used by ttgLib.
	typedef ttg::Error Error;
}

#ifdef TTG_USING_NAMESPACES
using namespace ttg;
#endif

#endif
